package com.maxuxu.scihome.controller.document;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @className: DocumentDownloadController
 * @author: Maxuxu
 * @date: 2022/7/5 23:04
 * @description:
 */
@RestController
@Slf4j
public class DocumentDownloadController {
    // @GetMapping("/document/download")
    // public String download(@RequestParam("filename")  String filename, HttpServletRequest request){
    //     String filepath = request.getRequestURL() + "/source/" + filename;
    //     System.out.println(filepath);
    //     return filepath;
    // }
    @GetMapping("/document/download")
    public String downloads(@RequestParam("filename")  String filename, HttpServletRequest request,
                            HttpServletResponse response) throws IOException {
        if (filename != null) {
            //设置文件路径
            File file = new File("D://test//file//"+filename);
            //File file = new File(realPath , fileName);
            if (file.exists()) {
                // 设置强制下载不打开
                response.setContentType("application/force-download");
                // 6.添加http头信息
                // 因为fileName的编码格式是UTF-8 但是http头信息只识别 ISO8859-1 的编码格式
                // 因此要对fileName重新编码
                try {
                    response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("UTF-8"), "ISO8859-1"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return "下载成功";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "下载失败";
    }

}
